package com.srwl.mytx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.R;
import com.srwl.mytx.domain.Topic;
import com.srwl.mytx.utils.GlideLoadUtils;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by dahai2070 on 2017/2/17 0017.
 */
public class SellingAdapter extends RecyclerView.Adapter<SellingAdapter.SellingAdapterHolder> {
    private Context mContext;
    private List<Topic> goodsList;

    public SellingAdapter(List<Topic> goodsList, Context mContext) {
        this.goodsList = goodsList;
        this.mContext = mContext;
    }

    @Override

    public SellingAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.tx_my_business_selling, parent, false);
        return new SellingAdapterHolder(view);
    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }

    @Override
    public void onBindViewHolder(SellingAdapterHolder holder, final int position) {

        final Topic goods = goodsList.get(position);
//        RequestOptions options=new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate();
//        Glide.with(mContext).load(goods.getPictures().get(0)).apply(options).into(holder.ivGoodsPicture);
        List<String> pictures = goods.getPictures();
        if (pictures != null && pictures.size() != 0) {
            GlideLoadUtils.glideLoad(mContext, pictures.get(0), holder.ivGoodsPicture);
        }
        holder.tvGoodsName.setText(goods.getTitle());
        holder.tvUpdateDate.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(goods.getUpdated()));
        holder.btnMoreSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2017/2/20 0020 用监听 打开dialog
                if (clickListener != null) {
                    clickListener.moreSelectClick(position);
                }
            }
        });

    }

    class SellingAdapterHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_goods_picture)
        ImageView ivGoodsPicture;
        @BindView(R.id.tv_title)
        TextView tvGoodsName;
        @BindView(R.id.btn_more_select)
        ImageButton btnMoreSelect;
        @BindView(R.id.tv_update_date)
        TextView tvUpdateDate;

        public SellingAdapterHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnSellingClickListener {
        void moreSelectClick(int position);
    }

    private OnSellingClickListener clickListener;

    public void setOnSellingClickListener(OnSellingClickListener clickListener) {
        this.clickListener = clickListener;
    }
}
